<template>
  <el-dialog
    :modal="false"
    title="请选择预览打印的方式(A4纸每页打印6个标签)"
    :visible.sync="centerDialogVisible"
    width="30%"
    center
  >
    <!-- <div > -->
    <el-row
      v-loading.fullscreen="fullscreenLoading"
      :gutter="16"
      class="btn-box"
    >
      <el-col :xl="6" :lg="12" :md="12">
        <el-button type="success" size="mini" @click="printWaybillLabel10">
          10x10
        </el-button>
      </el-col>
      <el-col :xl="6" :lg="12" :md="12">
        <el-button type="success" size="mini" @click="printWaybillLabelA4">
          A4
        </el-button>
      </el-col>
      <el-col :xl="6" :lg="12" :md="12">
        <el-button type="success" size="mini" @click="createPdfLabel10">
          10x10(PDF)
        </el-button>
      </el-col>
      <el-col :xl="6" :lg="12" :md="12">
        <el-button type="success" size="mini" @click="createPdfLabelA4">
          A4(PDF)
        </el-button>
      </el-col>
    </el-row>
    <!-- </div> -->
  </el-dialog>
</template>

<script>
import { ajax } from "@/store/yg/common/utils";
import axios from "axios";
import * as constant from "@/store/yg/common/constant";
import { m } from "@/store/yg/common/local";
import { getLodop } from "@/store/yg/common/LodopFuncs";

export default {
  props: ["row", "data", "more"],
  data() {
    return {
      fullscreenLoading: false,
      centerDialogVisible: false
      // LODOP: getLodop()
    };
  },

  methods: {
    printWaybillLabel10() {
      this.centerDialogVisible = false;

      var waybillIds = [];
      var id = this.data["id"];
      waybillIds.push(id);
      this.addAmbNoteTranslate(waybillIds);
      this._printWaybillLabel10(waybillIds, "WL", "", "", 0);
    },

    printWaybillLabelA4() {
      this.centerDialogVisible = false;

      var waybillIds = [];
      var id = this.data["id"];
      waybillIds.push(id);
      this.addAmbNoteTranslate(waybillIds);
      this._printWaybillLabel10(waybillIds, "WL", "", "", 1);
    },

    createPdfLabel10() {
      this.centerDialogVisible = false;
      var param = {};
      var waybillIds = [];
      var id = this.data["id"];
      waybillIds.push(id);
      param["waybillIds"] = waybillIds;
      this.addAmbNoteTranslate(waybillIds);
      this.fullscreenLoading = true;
      ajax("createPdfLabel10x10", param).then(
        res => {
          this.fullscreenLoading = false;
          if (null != res.errorMesage && res.errorMesage != "") {
            this.$message.error(m(res.errorMesage));
          } else {
            var path = (path = constant.FILE_SERVER + res.path);
            var fileUrl = path + res.fileName;

            var path = (path = constant.FILE_SERVER + "/" + res.data);
            location.href =
              constant.API_SERVER +
              "/open/downloadFileByHttp?fileName=" +
              res.fileName +
              "&path=" +
              fileUrl;
            // console.log(fileUrl);
            // window.open(fileUrl);
          }
        },
        error => {
          console.log(error);
        }
      );
    },

    createPdfLabelA4() {
      this.centerDialogVisible = false;
      var param = {};
      var waybillIds = [];
      var id = this.data["id"];
      waybillIds.push(id);
      param["waybillIds"] = waybillIds;
      this.addAmbNoteTranslate(waybillIds);
      this.fullscreenLoading = true;
      ajax("createPdfLabelA4", param).then(
        res => {
          this.fullscreenLoading = false;
          if (null != res.errorMesage && res.errorMesage != "") {
            this.$message.error(m(res.errorMesage));
          } else {
            var path = (path = constant.FILE_SERVER + res.path);
            var fileUrl = path + res.fileName;
            var path = (path = constant.FILE_SERVER + "/" + res.data);
            location.href =
              constant.API_SERVER +
              "/open/downloadFileByHttp?fileName=" +
              res.fileName +
              "&path=" +
              fileUrl;
            // console.log(fileUrl);
            // window.open(fileUrl);
          }
        },
        error => {
          console.log(error);
        }
      );
    },

    _printWaybillLabel10(
      waybillId,
      surfaceSingleCode,
      boxNo,
      printName,
      printType
    ) {
      var param = {};
      param["waybillIds"] = waybillId;
      param["surfaceSingleCode"] = surfaceSingleCode;
      param["boxNo"] = boxNo;
      this.fullscreenLoading = true;
      ajax("printWaybillLabel", param).then(
        res => {
          console.log(res);
          this.fullscreenLoading = false;
          if (res.code != 200) {
            if (null != res.message && res.message != "") {
              this.$message.error(m(res.message));
            }
          } else {
            const htmls = res.data;
            if (printType == 0) {
              console.info("打印HTML面单10x10");
              this._print10(htmls, printName);
            } else if (printType == 1) {
              console.info("打印HTML面单A4");
              this._printWaybillLabelA4(htmls, printName);
            }
          }
        },
        error => {
          console.log(error);
        }
      );
    },

    // LODOP 插件打印 A4
    _printWaybillLabelA4(htmls, printName) {
      if (null == htmls || typeof htmls === "undefined") {
        return;
      }
      let LODOP = getLodop();
      console.log("LODOP :>> ", LODOP);
      LODOP.PRINT_INIT("打印订单信息");

      LODOP.SET_PRINT_PAGESIZE(1, 0, 0, "A4");
      var pageNum = Math.ceil(htmls.length / 6);
      var htmlLength = htmls.length;
      for (var j = 0; j < pageNum; j++) {
        var n = 6 * j;
        for (var m = 0; m < 6; m++) {
          var k = m + n;
          if (k >= htmlLength) {
            continue;
          }
          var html = htmls[k];
          if (m == 0) {
            LODOP.ADD_PRINT_HTM(0, 0, "94mm", "94mm", html); // Top,Left,Width,Height,strHtmlContent
          } else if (m == 1) {
            LODOP.ADD_PRINT_HTM(0, "100mm", "94mm", "94mm", html);
          } else if (m == 2) {
            LODOP.ADD_PRINT_HTM("97mm", 0, "94mm", "94mm", html);
          } else if (m == 3) {
            LODOP.ADD_PRINT_HTM("97mm", "100mm", "94mm", "94mm", html);
          } else if (m == 4) {
            LODOP.ADD_PRINT_HTM("194mm", 0, "94mm", "94mm", html);
          } else if (m == 5) {
            LODOP.ADD_PRINT_HTM("194mm", "100mm", "94mm", "94mm", html);
          }
          var body = document.querySelector("body");
          body.append(
            "<div id='templateContentHtml' style='display:none'>" +
              html +
              "</div>"
          );
          var barCodes = document.getElementsByName("barcode");
          for (var i = 0; i < barCodes.length; i++) {
            var barCode = barCodes[i];
            var value = barCode.getAttribute("value");
            var values;
            if (value.indexOf("|") >= 0) {
              values = barCode.getAttribute("value").split("|");
            } else {
              values = barCode.getAttribute("value").split("-");
            }
            var reg = new RegExp("mm", "g");
            if (values[5] != "" && values[5] != null) {
              var codes = values[5].split("+");
              var serialNo = codes[0].substring(6, codes[0].length);
              var top = values[0].replace(reg, "").trim();
              var left = values[1].replace(reg, "").trim();
              left = parseFloat(left);
              top = parseFloat(top);
              if (m == 0) {
                if (i == 1) {
                  LODOP.SET_PRINT_STYLE("FontSize", 10);
                  LODOP.SET_PRINT_STYLE("Bold", 1);
                  LODOP.ADD_PRINT_TEXT(
                    295,
                    left - 5 + "mm",
                    60,
                    10,
                    codes[0].substring(0, 6)
                  );
                  LODOP.SET_PRINT_STYLE("FontSize", 16);
                  LODOP.SET_PRINT_STYLE("Bold", 1);
                  LODOP.ADD_PRINT_TEXT(290, left + 9 + "mm", 100, 10, serialNo);
                  LODOP.SET_PRINT_STYLE("FontSize", 10);
                  LODOP.SET_PRINT_STYLE("Bold", 1);
                  LODOP.ADD_PRINT_TEXT(
                    295,
                    left + 29 + "mm",
                    40,
                    10,
                    "+" + codes[1]
                  );
                }
                left = left - 5 + "mm";
                LODOP.ADD_PRINT_BARCODE(
                  values[0],
                  left,
                  values[2],
                  values[3],
                  values[4],
                  values[5]
                );
              } else if (m == 1) {
                if (i == 1) {
                  LODOP.SET_PRINT_STYLE("FontSize", 10);
                  LODOP.SET_PRINT_STYLE("Bold", 1);
                  LODOP.ADD_PRINT_TEXT(
                    295,
                    left + 95 + "mm",
                    60,
                    10,
                    codes[0].substring(0, 6)
                  );
                  LODOP.SET_PRINT_STYLE("FontSize", 16);
                  LODOP.SET_PRINT_STYLE("Bold", 1);
                  LODOP.ADD_PRINT_TEXT(
                    290,
                    left + 109 + "mm",
                    100,
                    10,
                    serialNo
                  );
                  LODOP.SET_PRINT_STYLE("FontSize", 10);
                  LODOP.SET_PRINT_STYLE("Bold", 1);
                  LODOP.ADD_PRINT_TEXT(
                    295,
                    left + 129 + "mm",
                    40,
                    10,
                    "+" + codes[1]
                  );
                }
                left = left + 95 + "mm";
                LODOP.ADD_PRINT_BARCODE(
                  values[0],
                  left,
                  values[2],
                  values[3],
                  values[4],
                  values[5]
                );
              } else if (m == 2) {
                if (i == 1) {
                  var codes = values[5].split("+");
                  var serialNo = codes[0].substring(6, codes[0].length);
                  LODOP.SET_PRINT_STYLE("FontSize", 10);
                  LODOP.SET_PRINT_STYLE("Bold", 1);
                  LODOP.ADD_PRINT_TEXT(
                    661,
                    left - 5 + "mm",
                    60,
                    10,
                    codes[0].substring(0, 6)
                  );
                  LODOP.SET_PRINT_STYLE("FontSize", 16);
                  LODOP.SET_PRINT_STYLE("Bold", 1);
                  LODOP.ADD_PRINT_TEXT(656, left + 9 + "mm", 100, 10, serialNo);
                  LODOP.SET_PRINT_STYLE("FontSize", 10);
                  LODOP.SET_PRINT_STYLE("Bold", 1);
                  LODOP.ADD_PRINT_TEXT(
                    661,
                    left + 29 + "mm",
                    40,
                    10,
                    "+" + codes[1]
                  );
                }
                top = top + 97 + "mm";
                left = left - 5 + "mm";
                LODOP.ADD_PRINT_BARCODE(
                  top,
                  left,
                  values[2],
                  values[3],
                  values[4],
                  values[5]
                );
              } else if (m == 3) {
                if (i == 1) {
                  var codes = values[5].split("+");
                  var serialNo = codes[0].substring(6, codes[0].length);
                  LODOP.SET_PRINT_STYLE("FontSize", 10);
                  LODOP.SET_PRINT_STYLE("Bold", 1);
                  LODOP.ADD_PRINT_TEXT(
                    661,
                    left + 95 + "mm",
                    60,
                    10,
                    codes[0].substring(0, 6)
                  );
                  LODOP.SET_PRINT_STYLE("FontSize", 16);
                  LODOP.SET_PRINT_STYLE("Bold", 1);
                  LODOP.ADD_PRINT_TEXT(
                    656,
                    left + 109 + "mm",
                    100,
                    10,
                    serialNo
                  );
                  LODOP.SET_PRINT_STYLE("FontSize", 10);
                  LODOP.SET_PRINT_STYLE("Bold", 1);
                  LODOP.ADD_PRINT_TEXT(
                    661,
                    left + 129 + "mm",
                    40,
                    10,
                    "+" + codes[1]
                  );
                }
                left = left + 95 + "mm";
                top = top + 97 + "mm";
                LODOP.ADD_PRINT_BARCODE(
                  top,
                  left,
                  values[2],
                  values[3],
                  values[4],
                  values[5]
                );
              } else if (m == 4) {
                if (i == 1) {
                  var codes = values[5].split("+");
                  var serialNo = codes[0].substring(6, codes[0].length);
                  LODOP.SET_PRINT_STYLE("FontSize", 10);
                  LODOP.SET_PRINT_STYLE("Bold", 1);
                  LODOP.ADD_PRINT_TEXT(
                    1028,
                    left - 5 + "mm",
                    60,
                    10,
                    codes[0].substring(0, 6)
                  );
                  LODOP.SET_PRINT_STYLE("FontSize", 14);
                  LODOP.SET_PRINT_STYLE("Bold", 1);
                  LODOP.ADD_PRINT_TEXT(
                    1024,
                    left + 9 + "mm",
                    100,
                    10,
                    serialNo
                  );
                  LODOP.SET_PRINT_STYLE("FontSize", 10);
                  LODOP.SET_PRINT_STYLE("Bold", 1);
                  LODOP.ADD_PRINT_TEXT(
                    1028,
                    left + 28 + "mm",
                    40,
                    10,
                    "+" + codes[1]
                  );
                }
                top = top + 194 + "mm";
                left = left - 5 + "mm";
                LODOP.ADD_PRINT_BARCODE(
                  top,
                  left,
                  values[2],
                  values[3],
                  values[4],
                  values[5]
                );
              } else if (m == 5) {
                if (i == 1) {
                  var codes = values[5].split("+");
                  var serialNo = codes[0].substring(6, codes[0].length);
                  LODOP.SET_PRINT_STYLE("FontSize", 10);
                  LODOP.SET_PRINT_STYLE("Bold", 1);
                  LODOP.ADD_PRINT_TEXT(
                    1028,
                    left + 95 + "mm",
                    60,
                    10,
                    codes[0].substring(0, 6)
                  );
                  LODOP.SET_PRINT_STYLE("FontSize", 14);
                  LODOP.SET_PRINT_STYLE("Bold", 1);
                  LODOP.ADD_PRINT_TEXT(
                    1024,
                    left + 109 + "mm",
                    100,
                    10,
                    serialNo
                  );
                  LODOP.SET_PRINT_STYLE("FontSize", 10);
                  LODOP.SET_PRINT_STYLE("Bold", 1);
                  LODOP.ADD_PRINT_TEXT(
                    1028,
                    left + 128 + "mm",
                    40,
                    10,
                    "+" + codes[1]
                  );
                }
                left = left + 95 + "mm";
                top = top + 194 + "mm";
                LODOP.ADD_PRINT_BARCODE(
                  top,
                  left,
                  values[2],
                  values[3],
                  values[4],
                  values[5]
                );
              }
            }
            if (
              values[6] == 1 ||
              typeof values[6] == "undefined" ||
              values[6] == ""
            ) {
              if (i == 1) {
                LODOP.SET_PRINT_STYLEA(0, "ShowBarText", 0);
              } else {
                LODOP.SET_PRINT_STYLEA(0, "ShowBarText", 1);
              }
              LODOP.SET_PRINT_STYLEA(0, "AlignJustify", 2);
            } else {
              LODOP.SET_PRINT_STYLEA(0, "ShowBarText", 0);
            }
          }
          // $("#templateContentHtml").remove();
          var templateContentHtml = document.querySelector(
            "templateContentHtml"
          );
          this.removeDom(templateContentHtml);
        }
        LODOP.NewPage(); //分页
      }
      if (null != printName && printName != "") {
        LODOP.SET_PRINTER_INDEX(printName);
      } else {
        LODOP.PREVIEW();
      }
    },
    // LODOP 插件打印HTML
    _print10(htmls, printName) {
      if (null == htmls || typeof htmls === "undefined") {
        return;
      }
      console.log("标签打印中" + printName + ",htmls.size=" + htmls.length);
      console.log("object :>> ", getLodop);
      let LODOP = getLodop();
      // let LODOP = this.LODOP;
      console.log("LODOP :>> ", LODOP);
      LODOP.PRINT_INIT("打印订单信息");
      LODOP.SET_PRINT_PAGESIZE(1, "100mm", "100mm", "CreateCustomPage");
      for (var j = 0; j < htmls.length; j++) {
        var html = htmls[j];
        LODOP.ADD_PRINT_HTM(0, 0, "100mm", "98mm", html);
        var body = document.querySelector("body");
        body.append(
          "<div id='templateContentHtml' style='display:none'>" +
            html +
            "</div>"
        );
        var barCodes = document.getElementsByName("barcode");
        for (var i = 0; i < barCodes.length; i++) {
          var barCode = barCodes[i];
          var value = barCode.getAttribute("value");
          var values;
          if (value.indexOf("|") >= 0) {
            values = barCode.getAttribute("value").split("|");
          } else {
            values = barCode.getAttribute("value").split("-");
          }
          LODOP.ADD_PRINT_BARCODE(
            values[0],
            values[1],
            values[2],
            values[3],
            values[4],
            values[5]
          );
          LODOP.SET_PRINT_STYLEA(0, "ShowBarText", 1);
          if (values[6] == 1 || typeof values[6] == "undefined") {
            LODOP.SET_PRINT_STYLEA(0, "AlignJustify", 2);
          } else {
            LODOP.SET_PRINT_STYLEA(0, "ShowBarText", 0);
          }
        }
        // $("#templateContentHtml").remove();
        var templateContentHtml = document.querySelector("templateContentHtml");
        this.removeDom(templateContentHtml);
        LODOP.NewPage(); //分页
      }
      if (null != printName && printName != "") {
        LODOP.SET_PRINTER_INDEX(printName);
        LODOP.PRINT();
      } else {
        LODOP.PREVIEW();
      }
    },

    // 移除节点
    removeDom(model) {
      var self = this;
      if (self.$parent.model) {
        self.$parent.model.children.forEach((item, index) => {
          if (item.name == model.name) {
            self.$parent.model.children.splice(index, 1);
          }
        });
      }
    },

    /**
     * @param waybillId
     * 点击之前生成费用
     * 更新仓库打印标签状态
     * @returns
     */

    addAmbNoteTranslate(ids) {
      var param = {};
      param["waybillIds"] = ids;
      ajax("addAmbNoteTranslate", param).then(
        res => {
          console.log(res);
          if (res.code != 200) {
          } else {
          }
        },
        error => {
          console.log(error);
        }
      );
    }
  },
  mounted() {}
};
</script>

<style lang="scss" scoped>
.btn-box {
  .el-button {
    width: 100%;
    margin-top: 10px;
  }
}
</style>
